package wfmanage.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import edu.wfmanage.models.HelpDeskUser;
import edu.wfmanage.models.Ticket;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

public class HelpDesk_Login extends ActionSupport {

	 private String username = "";
	 private String password = "";
	 private String commandButton = "";
	 private ArrayList<Ticket> tickets;
	 private String status;
	 
	 private int userid;
	 private String email = "";
	 private String phone = "";
	 
	 private ArrayList<String> supports;
	  
	 
	public ArrayList<String> getSupports() {
		return supports;
	}
	public void setSupports(ArrayList<String> supports) {
		this.supports = supports;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public ArrayList<Ticket> getTickets() {
		return tickets;
	}
	public void setTickets(ArrayList<Ticket> tickets) {
		this.tickets = tickets;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	 
	 public int getUserid() {
		return userid;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String execute(){
		 
		 		
		  ResultSet result = null;
		  Connection con = DB.getConnection();
		  String query = "select * from user where user_name = '" + username + "' and password = '" + password + "';";
		  result = DB.readFromDB(query, con);
		  try{
			  while(result.next()){
				  userid = result.getInt("user_id");
				  email = result.getString("email_id");
				  phone = result.getString("phone_no");
				  setUserid(userid);
				  setEmail(email);
				  setPhone(phone);
			  }
			     Map<String, Object> session = ActionContext.getContext().getSession();
				 HelpDeskUser helpuser = new HelpDeskUser();
				 helpuser.setUserid(userid);
				 helpuser.setUsername(username);
				 helpuser.setPassword(password);
				 helpuser.setEmailid(email);
				 helpuser.setPhone(phone);
					if(session.get("helpuser") != null && session.get("helpuser") != "")
						helpuser = (HelpDeskUser) session.get("helpuser");
					if (helpuser == null) {
						helpuser = new HelpDeskUser();
					}
					session.put("helpuser", helpuser);
					
					
		  }catch(SQLException ex){
			  ex.printStackTrace();
		  }
		  
		 if(this.username.isEmpty())
		   return "initial_entry";
		 
		 String value;
		 value = HelpDesk_Login.validateCredentials(username, password);
		 
		 if(value=="error")
			 return value;
		 
		 value = HelpDesk_Login.validateRole(username, password);
		 if(value=="role_unassigned")
			 return value;
		 
		 String rolename = HelpDesk_Login.findRole(username, password);
		 MyLog.log(rolename);
		 if(rolename.equals("HelpDesk_Admin")){
			 tickets = Ticket.find(", assignment a, role r where t.status_id = a.status_id and "
						+ "a.support_id = r.role_id and r.role_name = 'HelpDesk_Admin'");
			 supports = ViewTickets.findSupportExecutiveLevel();
			 status = ViewTickets.findStatus();
			 for(int i=0;i<tickets.size();i++){
			 for(String s: supports){
				 tickets.get(i).setSupport_level(supports.get(i));
				 tickets.get(i).setNames(ViewTickets.findNamesforRole(supports.get(i)));
			 }
		 }
			 for(Ticket t: tickets){
				 MyLog.log("Support Level: " + t.getSupport_level());
				 for(int i=0; i<t.getNames().size();i++){
					 MyLog.log("Name" + i + ": " + t.getNames().get(i));
				 }
			 }
			 		 
			 return "admin";
		 }
		 else if(rolename.equals("Support_Executive 1"))
		 {
			 tickets = Ticket.find(", assignment a, role r where t.status_id = a.status_id and "
						+ "a.support_id = r.role_id and r.role_name = 'Support_Executive 1';");
			 return "support_executive 1";
		 }
		 else if(rolename.equals("Support_Executive 2"))
		 {
			 tickets = Ticket.find(", assignment a, role r where t.status_id = a.status_id and "
						+ "a.support_id = r.role_id and r.role_name = 'Support_Executive 2';");
			 return "support_executive 2";
		 }
		 else if(rolename.equals("Support_Executive 3"))
		 {
			 tickets = Ticket.find(", assignment a, role r where t.status_id = a.status_id and "
						+ "a.support_id = r.role_id and r.role_name = 'Support_Executive 3';");
			 
			 return "support_executive 3";
		 }
		 else
			 return "user";
	 }
	 
	 public static String validateCredentials(String username, String password){
		 
		    ResultSet result = null;
	    	Connection con = DB.getConnection();
		    
	    	String query = "select user_id from user where user_name='" + username + "' and password ='" + password + "';";
	    	result = DB.readFromDB(query, con);
	    	MyLog.log("\nInside validateCredentials\n");
	    	try {
				if(!result.next()){
					return "error";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    	
		 return "success";
	 }
	 
	 public static String validateRole(String username, String password){
		 
		    ResultSet result = null;
	    	Connection con = DB.getConnection();
	    	int userid = 0;
	    	String query = "select user_id from user where user_name='" + username + "' and password ='" + password + "';";
	    	result = DB.readFromDB(query, con);
	    	
	    	try {
				while(result.next()){
					userid = result.getInt("user_id");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    	query = "select role_id from user_role where user_id = '" + userid + "';";
	    	result = DB.readFromDB(query, con);
	    	
	    	try {
				if(!result.next()){
					return "role_unassigned";
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    	
		 return "success";
	 }
	 
	 public static String findRole(String username, String password){
	    	ResultSet result = null;
	    	Connection con = DB.getConnection();
	    	String rolename = null;
	    	int userid = 0;
	    	String query = "select user_id from user where user_name='" + username + "' and password ='" + password + "';";
	    	result = DB.readFromDB(query, con);
	    	
	    	try {
				while(result.next()){
					userid = result.getInt("user_id");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    	query = "select role.role_name as role_name from user_role, role where role.role_id = user_role.role_id and user_role.user_id = '" + userid + "';";
	    	result = DB.readFromDB(query, con);
	    	
	    	try {
				while(result.next()){
					rolename = result.getString("role_name");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
	    		
	    	return rolename;
	    }

}
